//公共数据

// 使用前先引入 defineStore；
import { defineStore } from 'pinia'
//API
import publicApi from '@/api'

import $common from '@/utils/common';

// defineStore 方法有两个参数，第一个参数是模块化名字（相当于ID，不能重复）
// 第二个参数是选项
export const useCommonStore = defineStore('common', {
	// data存放模块的变量
	state() {
		return {
			//用来作为组件间传递
			commonData: null,
			mealCoay: [],	//拷贝的餐别数据

			//通用数据
			allRoomList: [], //全部食堂数据
			dishCateList: [], //全部菜品类别
			measureList: [], //全部计量单位数据
			cardTypeList: [], //获取全部卡号类型数据
			departList: [],	//全部组织架构数据
			dictList: [],	//字典数据

			//枚举数据
			EMealType: [], //餐别
			ECardDeductMode: [], //卡类消费扣款方式
			ECardOpType: [], //卡业务操作类型
			ECardRechargeDeduct: [], //充值扣卡类型
			ECardStatus: [], //卡片状态
			EConsumeMachineType: [], //消费机类型
			EConsumeMode: [], //卡消费流水类型
			EDataFromType: [], //数据来源
			EDeviceFaceStatus: [], //设备人脸更新模式
			EDeviceReadCardMode: [], //设备读卡类型
			EIdentityMode: [], //身份识别模式/预留
			EDPGConsumeMode: [], //大苹果设备默认启动模式
			EZKConsumeMode: [], //中控万傲设备消费默认模式
			EOrderStatus: [], //订餐订单状态
			EPayMode: [], //支付方式
			EPermissionType: [], //权限类型
			ERecognitionMode: [], //设备人脸识别模式
			ERuleMode: [], //规则类型
			ESubsidyMode: [], //补贴模式
			ESubsidyRecStatus: [], //补贴领取状态
			EWalletType: [], //设备使用钱包
			EWeek: [], //每周数据
			EBatchMode: [], //清零类型
			EClearMode: [], //清零数据
			ESyncCmdType: [], //同步万傲时间枚举
			ERmbUnit: [], //参数限制
			EDishType: [], //菜品类别，素菜/荤菜
		}
	},
	//计算属性
	getters: {

	},
	//methed方法
	actions: {
		//获取所有食堂数据
		getAllRoomList() {
			return new Promise((resolve, reject) => {
				if (this.allRoomList.length > 0) {
					resolve(this.allRoomList)
				} else {
					publicApi.basic.getAllRoomList().then((res) => {
						let data = res.data || []
						data.forEach(item => {
							item.label = item.RoomName
							item.value = item.ID
						})
						this.allRoomList = data
						resolve(data);
					}).catch((error) => {
						reject(error);
					});
				}
			});
		},

		//获取所有菜单品类数据
		getDishCatetList(params) {
			return new Promise((resolve, reject) => {
				publicApi.dishes.getDishCatetList(params).then((res) => {
					let data = res.data || []
					data.forEach(item => {
						item.label = item.CateName
						item.value = item.ID
					})
					this.dishCateList = data
					resolve(data);
				}).catch((error) => {
					reject(error);
				});
			});
		},

		//获取所有单位数据
		getMeasureList(params) {
			return new Promise((resolve, reject) => {
				publicApi.dishes.getMeasureList(params).then((res) => {
					let data = res.data || []
					data.forEach(item => {
						item.label = item.MeasureName
						item.value = item.ID
					})
					this.measureList = data
					resolve(data);
				}).catch((error) => {
					reject(error);
				});
			});
		},

		//获取所有卡号类型
		getCardTypeList(params = {}, key = 'ID') {
			return new Promise((resolve, reject) => {
				// if (this.cardTypeList.length > 0) {
				// 	resolve(this.cardTypeList);
				// } else {
					publicApi.basic.getCardTypeList(params).then((res) => {
						let data = res.data || []
						data.forEach(item => {
							item.label = `${item.CardTypeCode} ${item.CardTypeName}`
							item.value = item[key]
						})
						this.cardTypeList = data
						resolve(data);
					}).catch((error) => {
						reject(error);
					});
				// }
			});
		},

		//获取所有组织架构
		getAllDepartList() {
			return new Promise((resolve, reject) => {
				if (this.departList.length > 0) {
					resolve(this.departList)
				} else {
					publicApi.comm.getDepartTree().then((res) => {
						let data = res.data || []
						this.departList = data
						resolve(data);
					}).catch((error) => {
						reject(error);
					});
				}
			});
		},

		//获取所有字典类型数据
		getAllDictList() {
			return new Promise((resolve, reject) => {
				if (this.dictList.length > 0) {
					resolve(this.dictList)
				} else {
					publicApi.comm.getDictTree().then((res) => {
						let data = res.data || []
						this.dictList = data
						resolve(data);
					}).catch((error) => {
						reject(error);
					});
				}
			});
		},


		//========================================= 枚举数据 =========================================
		//获取枚举数据 params 枚举类型
		getEnumTypes(params, valueName = 'Key') {
			return new Promise((resolve, reject) => {
				if (this[params]?.length > 0) {
					resolve(this[params])
				} else {
					publicApi.comm.getEnumTypes(params).then((res) => {
						let list = []
						res.data[params]?.forEach((item) => {
							list.push({ label: item.Desc, value: item[valueName] });
						});
						this[params] = list
						resolve(list)
					}).catch((error) => {
						reject(error)
					});
				}
			});
		},

	},
})
